var tarBtn = RichBase.extend({
	statics : {},
	EVENTS : {

		"click" : {
			".submit_btn" : "sub"
		}

	}, 
	init : function(opt){                                    },
	sub : function(that,e){
		var postData = $("form#qunaer_form").serialize();
		//console.log(postData)
		$.ajax({
			//type:'POST',url: 'http://www.12301.cc/admin/pppp.php',data: postData, dataType:'json',
			type:'POST',url: 'call/jh_tuan.php?action=set_conf',data: postData, dataType:'json',
		}).done(function(res) {
			if(res.status=="success"){
				PFT_GLOBAL.U.Alert("success",'<p style="width:120px">'+res.msg+'</p>');
				
				$(".clickBox").each(function(){
					var on_type = $(this).attr("on");
					if(on_type=="1"){
						$(this).attr("data-bind","1");
					}	
				})
				//alert(res.msg)
			}
			else{
				PFT_GLOBAL.U.Alert("fail",'<p style="width:120px">'+res.msg+'</p>');
			}
		})
	},
	BtnClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		tarBtn.attr("data","0");
		var hi = tarBtn.attr("data-i")
		var active_list_a = tarBtn;
		$(".ticket_c").each(function(){
			var active = $(this).attr("data");
			//alert(active)
			if(active=="0"){ 
				$(this).css('color','');
				$(this).addClass("blue");
				$(this).find('.select').css("display","none");
				$(this).find('.selected').css({
					"display":"block",
					"text-decoration":"none",
					"font-size":"1.1em"
				});
				tarBtn.attr("data","1");
				}
			else{
				$(this).css('color','#999');
				$(this).removeClass("blue");
				$(this).find('.select').css("display","block");
				$(this).find('.selected').css("display","none");
			}
		});
		
		
		
	},
	
	
	
});
var search_all = RichBase.extend({
	
	statics : {},
	EVENTS : {
		"focus" : {
			".formInp" : "onInputFocus"
		},
		"blur" : {
			".formInp" : "onInputBlur"
		},
		"click" : {
			// ".ticket_c" : "BtnClick"
			".coutTd .countBtn" : "onCountBtnClick",
			".coutTd .countBtn" : "onCountBtnClick",
			".ticket_c" : "BtnClick"
			//".clickBox" : "clickBox"
			}
	},
	init : function(opt){
		this.onload();
	},
	
	onload : function(that,e){
		
		var that = this;
		$('#prodSearchInp').text('');
		$(".cleartxt").click(function(){
			$('#prodSearchInp').val('');
			$(this).css("display","none");
			$("#lid_id").val('');
		});
		$("#prodSearchInp").keyup(function(){
				
			var sth=$('#prodSearchInp').val();
			var i=0;
			
			$("#prodList li").each(function(){
		
				var n=$(this).text().indexOf(sth);
				
				if(n==-1){
					$(this).css("display","none");
					
				}else{
					i+=1
					$(this).css("display","block");
				}
			});
			if(i==0){
				//PFT_GLOBAL.U.Alert("success",'<p style="width:120px">抱歉暂无匹配搜索结果</p>');
				$("#prodList").css("display","none");
				
			}else{
				//$(".searchmsg").css("display","none");
				$("#prodList").css("display","block");
			} 
		});
		PFT.Ajax({
			url : "api/inside/saleProds.php?act=lands",
			type : "post",
			dataType : "json",
		},function(res){
			
			that.list_search(res); 
		})
	},
	
	
	
	list_search : function(data){
	var that = this;
	var list = data.lists;
	for(var i=0;i<list.length;i++){
	    $("#prodList").append("<li data="+list[i].id+">"+list[i].title+"</li>");
	}
	$("#prodList li").eq(0).addClass("setbg");
	$("#prodList li").click(function(){
	
		$(".must_sec").css("display","none");
		var i=$(this).index();
		$("#prodSearchInp").val(list[i].title);
		$("#prodList li").removeClass("setbg");
		$("#prodList li").eq(i).addClass("setbg");
		$("#prodList").css("display","none"); 
		$("#lid_id").val(list[i].id);
		var lid_b = list[i].id;
		//console.log(lid_b)
		that.data_list(lid_b);
        $(".cleartxt").css("display","block");
	});
	$("#prodSearchInp").click(function(){
        if($("#prodList").css("display")=='block')
            $("#prodList").css("display","none");
			
        else
		    $("#prodList").css("display","block");
			$(".tbBind").hide();
		});
	},
	//新加入的搜索 展示
	data_list : function(opt){
		var that = this;
		var opt = opt || {};
		var conf_type= Number($(".conf_type").val());
		var data = {
			//lid:7036
			lid : opt,
			conf_type : conf_type
		};
		var url = "qunaer.html?act=ticket";
		var url_id ="1";
		var listUl = $("#mlistUl");
		var pagenavW = $("#pagenavW");
		var status = this.statics.queryState;
		PFT.Ajax({
			//url : "product_package.html", 
			url : url,
			type : "GET",
			dataType : "json",
			data : data
		},function(res){
			var code = res.status;
			var lists = res.lists;
			//console.log(lists)
			var last = res.last;
			$(".submit_pay").show();
			var total = res.total;
			if(total>5){
				$(".show_class").css("display","block")
			}
			if(code=="success"){
					var conf_type = $(".conf_type").val();
					//console.log(conf_type)
					if(conf_type=="0"){
						var type = "0";
						that.buildHtml(res,type);
					}
					else if(conf_type=="1"){
						var type = "1";
						that.buildHtml(res,type);
					}
					else{
						var type = "2";
						that.buildHtml(res,type);
					}
				}else{
					
					listUl.html(status["empty"]);
					if(provice) that.fire("search_empty",res);
				}
		})
	},
	data_limit : function(){
		$(".ticket_con").animate({height:"285px"});
		$(".ticket_con").addClass("overflow_h");
		$(".show_class").text("显示全部");
	},
	 data_open : function(){
	 
	},
	buildHtml : function(res,type){
		//console.log(type)
		var that = this;
		
		var code = res.code;
		var lists = res.lists;
		
		
		var length = res.length;
		if(length=="0"){
			$(".ticket_con").css("display","none");
		}
		else{
		$(".ticket_con").css("display","block");
		}
		var str = "";
		var item_a = $("#itemTpl").html();
		var item_div = $("#itemTpl_tr").html();
		var item_con = $("#itemTpl_con").html();
		var result = "";
		var listUl = $("#mlistUl");
		var pagenavW = $("#pagenavW");
		var type = Number(type);
		var data = lists;
			str += '<table class="tbBind" border="0" cellpadding="0" cellspacing="0">';
			str += '<thead class="thead_icon">';
			str += '  <tr>';
			str += '	<th class="supplierdd">供应商产品ID</th>';
			str += '	<th class="ticketed">门票类型</th>';
			str += '	<th class="setWidth">建议零售价</th>';
			str += '	<th class="setWidth">结算价</th>';
            // str += '    <th class="setWidth">状态</th>';
            str += '    <th class="setWidth">绑定选择</th>';
			str += '  </tr>';
			str += '</thead>';
		if(type=="0"){
			for(var i in data){
				is_data_empty = false;
				str += '<tbody class="tbody">';
				str += '    <tr>';
				str += '		<input type="hidden" class="idsname" ids="'+data[i]["tid"]+'"   value="'+data[i]["tid"]+'_'+data[i]["aid"]+'">';
				//str += '		<input type="hidden" class="idstid"  value="'+data[i]["tid"]+'">';
				str += '		<td>'+data[i]["tid"]+'_'+data[i]["aid"]+'</td>';
				str += '		<td>'+data[i]["ttitle"]+'</td>';
				str += '		<td>'+data[i]["uprice"]+'</td>';
				str += '		<td class="setColor">'+data[i]["jsprice"]+'</td>';
				// str += '		<td>'+data[i]["bind"]+'</td>';
				str += '		<td><span class="clickBox" data-bid="'+data[i]["bid_a"]+'" data-bind="'+data[i]["bind_a"]+'" on="0"></span></td>';
				str += '	</tr>';
				str += '</tbody>';
				}
		}
		else if(type=="1"){
			for(var i in data){
			is_data_empty = false;
			str += '<tbody class="tbody">';
			str += '    <tr>';
			str += '		<input type="hidden" class="idsname" ids="'+data[i]["tid"]+'"  value="'+data[i]["aid"]+'|'+data[i]["lid"]+'|'+data[i]["tid"]+'">';
			//str += '		<input type="hidden" class="idstid"  value="'+data[i]["tid"]+'">';
			str += '		<td>'+data[i]["aid"]+'|'+data[i]["lid"]+'|'+data[i]["tid"]+'</td>';
			str += '		<td>'+data[i]["ttitle"]+'</td>';
			str += '		<td>'+data[i]["uprice"]+'</td>';
			str += '		<td class="setColor">'+data[i]["jsprice"]+'</td>';
			// str += '		<td>'+data[i]["bind"]+'</td>';
			str += '		<td><span class="clickBox" data-bid="'+data[i]["bid_b"]+'" data-bind="'+data[i]["bind_b"]+'" on="0"></span></td>';
			str += '	</tr>';
			str += '</tbody>';
			}
		}
		else{
			for(var i in data){
			is_data_empty = false;
			str += '<tbody class="tbody">';
			str += '    <tr>';
			str += '		<input type="hidden" class="idsname" ids="'+data[i]["tid"]+'"  value="'+data[i]["aid"]+'|'+data[i]["lid"]+'|'+data[i]["tid"]+'">';
			//str += '		<input type="hidden" class="idstid"  value="'+data[i]["tid"]+'">';
			str += '		<td>'+data[i]["aid"]+'|'+data[i]["lid"]+'|'+data[i]["tid"]+'</td>';
			str += '		<td>'+data[i]["ttitle"]+'</td>';
			str += '		<td>'+data[i]["uprice"]+'</td>';
			str += '		<td class="setColor">'+data[i]["jsprice"]+'</td>';
			// str += '		<td>'+data[i]["bind"]+'</td>';
			str += '		<td><span class="clickBox" data-bid="'+data[i]["bid_c"]+'" data-bind="'+data[i]["bind_c"]+'" on="0"></span></td>';
			str += '	</tr>';
			str += '</tbody>';
			}
			
			
		}
		str += '</table>';
		if(lists && lists.length > 0){}
		else{
			PFT_GLOBAL.U.Alert("success",'<p style="width:120px">抱歉暂无分销价格</p>');
			$(".thead_icon").css("display","none");
		}
		
		
		$(".tbBox").html(str);
		$(".clickBox").each(function(){
			//var tarBtn = $(e.currentTarget);
			var active = $(this).attr("data-bind");
			if(active=="1"){
				$(this).css("background","#0797D9");
				$(this).attr("on","1");
				var ids = $(this).parents(".tbody").find(".idsname").attr("ids");
				//console.log(ids)
				$(this).parents(".tbody").find(".idsname").attr("name",'ids['+ids+']');
				}
		});
	},
	BtnClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		tarBtn.attr("data","0");
		var hi = tarBtn.attr("data-i")
		var active_list_a = tarBtn;
		$(".ticket_c").each(function(){
			var active = $(this).attr("data");
			//alert(active)
			if(active=="0"){ 
				var aid = $(this).attr("data-aid");
				var tid = $(this).attr("data-tid");
				var lid = $(this).attr("data-lid");
				var price = $(this).attr("data-price");
				var n_price = price/100;
				$(".aid").val(aid);
				$(".tid").val(tid);
				$(".lid").val(lid);
				$(".n_price").val(n_price);
				$(this).css('color','');
				$(this).addClass("blue");
				var show_time = $(this).attr("data-time");
				$(".ticket_time_a").text(show_time);
				var num_count = $(this).find(".ticket_num").val()
				$(".count_num").val(num_count);
				$(this).find('.select').css("display","none");
				$(this).find('.selected').css({
					"display":"block",
					"text-decoration":"none",
					"font-size":"1.1em"
				});
				tarBtn.attr("data","1");
				}
			else{
				$(this).css('color','#999');
				$(this).removeClass("blue");
				$(this).find('.select').css("display","block");
				$(this).find('.selected').css("display","none");
			}
		});
		
		
		
	},
	});

var Product = RichBase.extend({
	statics : {},
	EVENTS : {
		"focus" : {
			".formInp" : "onInputFocus"
		},
		"blur" : {
			".formInp" : "onInputBlur"
		},
		"click" : {
			".coutTd .countBtn" : "onCountBtnClick"
		 }
	},
	init : function(opt){                                    },


});
new Product({container:$("#ticket_presale")});
$(".chose").click(function(){
	var rs=$(this).text();
	var pay=$(this).attr("data_type");
	if(pay=="2"){
		$(".admin_moeny").css("display","none");
		$(".yuan").css("display","none");
		$(".pay_style").val(pay);
	}
	else{
		$(".admin_moeny").css("display","block");
		$(".yuan").css("display","block");
		$(".pay_style").val(pay);
	}
	$(".pay_type").val(pay);
	$(".result").text(rs);
});

$(".chose").hover(function(){
	$(this).addClass("chosen");
},function(){
	$(this).removeClass("chosen");
});
new tarBtn({container:$(".content")});
new search_all({container:$("#qunaer_select")});
$(".clickBox").live("click",function(e){
	var tarBtn = $(e.currentTarget);
	var on_type = $(this).attr("on");
	var data_bind = $(this).attr("data-bind");
	var bid = $(this).attr("data-bid");
	if(data_bind=="1"){
		var r=confirm("确定要取消绑定吗？")
		  if (r==true)
			{
			  var typeData = {
					id:bid
				};
			  $.ajax({
				type:'POST',url: 'call/jh_tuan.php?action=remove',data: typeData, dataType:'json',
			}).done(function(res) {
				if(res.status=="success"){
					PFT_GLOBAL.U.Alert("fail",'<p style="width:120px">'+res.msg+'</p>');
					 tarBtn.attr("data-bind","0");
					 tarBtn.css("background","#FFFFFF");
				}
				else{
					PFT_GLOBAL.U.Alert("fail",'<p style="width:120px">'+res.msg+'</p>');
				}
			  })
			}
		  else
			{
				return false;
			}
		


	}
	else{
		//$(this).attr("data-bind","1");

	}
	if(on_type=="0"){
		$(this).attr("on","1");
		$(this).css("background","#0797D9");
		var ids = tarBtn.parents(".tbody").find(".idsname").attr("ids");
		//console.log(ids)
		tarBtn.parents(".tbody").find(".idsname").attr("name",'ids['+ids+']');
		//tarBtn.parents(".tbody").find(".idstid").attr("name","tid[]");
	}
	else{
		
		$(this).attr("on","0");
		$(this).css("background","#FFFFFF");
		tarBtn.parents(".tbody").find(".idsname").attr("name",null);
		//tarBtn.parents(".tbody").find(".idstid").attr("name",null);
	}
})

$(".qu").live("click",function(){
	$(".Identity").val("");
	$(".sign").val("");
	$(".conf_type").val("0");
	$(".qunaer_select").removeClass("mei_type");
	$(".qunaer_select").removeClass("baidu_type");
	$(".qunaer_select").addClass("qunaer_type");
	$(this).addClass("active");
	$(".mei").removeClass("active");
	$(".baidu").removeClass("active");
	$(".tbBind").hide();
	$(".first").text("供应商通信标识：");
	$(".secend").text("交互密钥：");
	$(".qu_supplia").text("（由去哪儿提供）");
	$(".qu_supplib").text("（由去哪儿提供）");
	$(".mei_supplir").show();
	var tye = $(".Identity").val();
	if(tye==""){
		$(".supplir_tod").removeClass("free");
		//$(".supplir_tod").attr("disabled",null);
		$(".supplir_tod").attr("readOnly",null);
	}
	else{
		$(".supplir_tod").addClass("free");
		//$(".supplir_tod").attr("disabled","ture");
		$(".supplir_tod").attr("readOnly","readonly");
	}
	var tyb = $(".sign").val();
	if(tyb==""){
		$(".dsign").removeClass("free");
		//$(".supplir_tod").attr("disabled",null);
		$(".supplir_tod").attr("readOnly",null);
	}
	else{
		$(".dsign").addClass("free");
		//$(".supplir_tod").attr("disabled","ture");
		$(".supplir_tod").attr("readOnly","readonly");
	}
	var typeData = {
			set_conf:"0"
		};
	$.ajax({
			type:'POST',url: 'call/jh_tuan.php?action=auth_information',data: typeData, dataType:'json',
		}).done(function(res) {
			if(res.status=="success"){
				$(".supplir_tod").val(res.supplierIdentity);
				$(".dsign").val(res.signkey);
				var typeo = res.signkey;
				if(typeo==""){
					$(".supplir_tod").removeClass("free");
					$(".Identity").attr("readonly",null);
					$(".sign").attr("readonly",null);
				}
				else{
					$(".supplir_tod").addClass("free");
					$(".Identity").attr("readonly","readonly");
					$(".sign").attr("readonly","readonly");
				}
			}
			else{
				//PFT_GLOBAL.U.Alert("fail",'<p style="width:120px">'+res.msg+'</p>');
			}
		})
	//new search_all({container:$("#qunaer_select")});
})

$(".mei").live("click",function(){
	$(".Identity").val("");
	$(".sign").val("");
	$(".qunaer_select").addClass("mei_type");
	$(".qunaer_select").removeClass("qunaer_type");
	$(".qunaer_select").removeClass("baidu_type");
	$(".conf_type").val("1");
	$(this).addClass("active");
	$(".qu").removeClass("active");
	$(".baidu").removeClass("active");
	$(".tbBind").hide();
	$(".first").text("供应商通信标识：");
	$(".secend").text("交互密钥：");
	$(".qu_supplia").text("（由美团提供：partnerId）");
	$(".qu_supplib").text("（由美团提供：clientID|clientsecret，注：以竖线隔开）");
	$(".mei_supplir").hide();
	var tye = $(".Identity").val();
	if(tye==""){
		$(".supplir_tod").removeClass("free");
		$(".supplir_tod").attr("readOnly",null);
	}
	else{
		$(".supplir_tod").addClass("free");
		$(".supplir_tod").attr("readOnly","readonly");
	}
	var tyb = $(".sign").val();
	if(tyb==""){
		$(".dsign").removeClass("free");
		$(".supplir_tod").attr("readOnly",null);
	}
	else{
		$(".dsign").addClass("free");
		$(".supplir_tod").attr("readOnly","readonly");
	}
	var typeData = {
			set_conf:"1"
		};
	$.ajax({
			type:'POST',url: 'call/jh_tuan.php?action=auth_information',data: typeData, dataType:'json',
		}).done(function(res) {
			if(res.status=="success"){
				$(".supplir_tod").val(res.supplierIdentity);
				$(".dsign").val(res.signkey);
				var typeo = res.signkey;
				if(typeo==""){ 
					$(".supplir_tod").removeClass("free");
					$(".Identity").attr("readonly",null);
					$(".sign").attr("readonly",null);
				}
				else{
					$(".supplir_tod").addClass("free");
					$(".Identity").attr("readonly","readonly");
					$(".sign").attr("readonly","readonly");
				}
			}
			else{
				//PFT_GLOBAL.U.Alert("fail",'<p style="width:120px">'+res.msg+'</p>');
			}
		})
	
})
$(".baidu").live("click",function(){
	$(".Identity").val("");
	$(".sign").val("");
	$(".qunaer_select").addClass("baidu_type");
	$(".qunaer_select").removeClass("mei_type");
	$(".qunaer_select").removeClass("qunaer_type");
	$(".conf_type").val("2");
	$(this).addClass("active");
	$(".qu").removeClass("active");
	$(".mei").removeClass("active");
	$(".first").text("服务商编号：");
	$(".secend").text("店铺编号|密钥：");
	$(".qu_supplia").text("（由百度直达号提供：prodiver_no）");
	$(".qu_supplib").text("（由百度直达号提供：sp_no|secret_key，注：以竖线隔开）");
	$(".mei_supplir").hide();
		var tye = $(".Identity").val();
	if(tye==""){
		$(".supplir_tod").removeClass("free");
		$(".supplir_tod").attr("readOnly",null);
	}
	else{
		$(".supplir_tod").addClass("free");
		$(".supplir_tod").attr("readOnly","readonly");
	}
	var tyb = $(".sign").val();
	if(tyb==""){
		$(".dsign").removeClass("free");
		$(".supplir_tod").attr("readOnly",null);
	}
	else{
		$(".dsign").addClass("free");
		$(".supplir_tod").attr("readOnly","readonly");
	}
	var typeData = {
			set_conf:"2"
		};
	$.ajax({
			type:'POST',url: 'call/jh_tuan.php?action=auth_information',data: typeData, dataType:'json',
		}).done(function(res) {
			if(res.status=="success"){
				$(".supplir_tod").val(res.supplierIdentity);
				$(".dsign").val(res.signkey);
				var typeo = res.signkey;
				if(typeo==""){ 
					$(".supplir_tod").removeClass("free");
					$(".Identity").attr("readonly",null);
					$(".sign").attr("readonly",null);
				}
				else{
					$(".supplir_tod").addClass("free");
					$(".Identity").attr("readonly","readonly");
					$(".sign").attr("readonly","readonly");
				}
			}
			else{
				//PFT_GLOBAL.U.Alert("fail",'<p style="width:120px">'+res.msg+'</p>');
			}
		})
	
})
$(document).ready(function(){ 
	var typeData = {
			set_conf:"0"
		};
	$.ajax({
			type:'POST',url: 'call/jh_tuan.php?action=auth_information',data: typeData, dataType:'json',
		}).done(function(res) {
			if(res.status=="success"){
				$(".Identity").val(res.supplierIdentity);
				$(".sign").val(res.signkey);
				var typeo = res.signkey;
				if(typeo==""){
					$(".supplir_tod").removeClass("free");
					//$(".supplir_tod").attr("disabled",null);
					$(".supplir_tod").attr("readOnly",null);
					
				}
				else{
					$(".supplir_tod").addClass("free");
					//$(".supplir_tod").attr("disabled","ture");
					$(".supplir_tod").attr("readOnly","readonly");
				}
			}
			else{
				PFT_GLOBAL.U.Alert("fail",'<p style="width:120px">'+res.msg+'</p>');
			}
		})
	var tye = $(".Identity").val();
	if(tye==""){
		$(".supplir_tod").removeClass("free");
		//$(".supplir_tod").attr("disabled",null);
		$(".supplir_tod").attr("readOnly",null);
	}
	else{
		$(".supplir_tod").addClass("free");
		//$(".supplir_tod").attr("disabled","ture");
		$(".supplir_tod").attr("readOnly","readonly");
	}
	var tyb = $(".sign").val();
	if(tyb==""){
		$(".dsign").removeClass("free");
		//$(".supplir_tod").attr("disabled",null);
		$(".supplir_tod").attr("readOnly",null);
	}
	else{
		$(".dsign").addClass("free");
		//$(".supplir_tod").attr("disabled","ture");
		$(".supplir_tod").attr("readOnly","readonly");
	}
	//var active_list_a = target.parents(".list_con").find('.list_data .yujbtn');
	


});

